package com.madarasan.SkiDroid;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.os.Bundle;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.preference.Preference.OnPreferenceClickListener;
import android.util.Log;

public class MyPreferencesActivity extends PreferenceActivity implements
		OnSharedPreferenceChangeListener {
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		addPreferencesFromResource(R.xml.preferences);
		// Get the custom preference
		Preference customPref = (Preference) findPreference("Limba preferata");
		final ListPreference listPreference = (ListPreference) getPreferenceManager()
				.findPreference("Limba preferata");
		Log.i("MyPreferencesActivity",
				"language is " + listPreference.getValue());
		customPref
				.setOnPreferenceClickListener(new OnPreferenceClickListener() {

					public boolean onPreferenceClick(Preference preference) {

						SharedPreferences customSharedPreference = getSharedPreferences(
								"Limba preferata", Activity.MODE_PRIVATE);
						SharedPreferences.Editor editor = customSharedPreference
								.edit();
						editor.putString("Limba preferata",
								listPreference.getValue());
						editor.commit();
						Log.i("MyPreferencesActivity", "now language is "
								+ listPreference.getValue());
						return true;
					}

				});
		Log.i("MyPreferencesActivity", "trying to return here");

	}

	@Override
	public void onPause() {
		super.onPause();
		Log.i("MyPreferencesActivity", "pausing and returning");
		startActivity(new Intent(this, MainActivity.class));

	}

	@Override
	public void onSharedPreferenceChanged(SharedPreferences arg0, String arg1) {
		Log.i("MyPreferencesActivity", "shared pref changed");
		Intent mainActivity = new Intent(this, MainActivity.class);
		startActivity(mainActivity);

	}
}